package jdbc;

import java.sql.*;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class UpdateDemo2 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url = "jdbc:mysql://localhost:3306/java76?useSSL=false";
        Connection con = null;
        PreparedStatement st = null;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入课程名：");
        String newName = sc.nextLine();
        //newName=newName.replaceAll("'","");
        System.out.println("新课时：");
        int hours = sc.nextInt();
        System.out.println("新年级号：");
        long gradeid = sc.nextLong();
        System.out.println("要改的课程编号：");
        long id = sc.nextLong();
        String sql = "update s_subject set subjectname=?,classhour=?,gradeid=? where id=?";
        System.out.println(sql);
        try {
            con = DriverManager.getConnection(url, "root", "root1234");

            st = con.prepareStatement(sql);
            st.setString(1,newName);
            st.setInt(2,hours);
            st.setLong(3,gradeid);
            st.setLong(4,id);
            int r = st.executeUpdate();
            System.out.println("修改了" + r + "条");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
